package com.sc.study.clazz.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sc.common.annotation.Log;
import com.sc.common.core.controller.BaseController;
import com.sc.common.core.domain.AjaxResult;
import com.sc.common.enums.BusinessType;
import com.sc.study.clazz.domain.ScClazz;
import com.sc.study.clazz.service.IScClazzService;
import com.sc.common.utils.poi.ExcelUtil;
import com.sc.common.core.page.TableDataInfo;

/**
 * 班级Controller
 * 
 * @author sc
 * @date 2022-02-27
 */
@RestController
@RequestMapping("/study/clazz")
public class ScClazzController extends BaseController
{
    @Autowired
    private IScClazzService scClazzService;

    /**
     * 查询班级列表包含年级 分页
     */
    @PreAuthorize("@ss.hasPermi('study:clazz:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScClazz scClazz)
    {
        startPage();
        List<ScClazz> list = scClazzService.selectScClazzList(scClazz);
        return getDataTable(list);
    }
    /**
     * 查询班级列表
     */
    @PreAuthorize("@ss.hasPermi('study:clazz:list_no_page')")
    @GetMapping("/listNoPage")
    public AjaxResult listNoPage(ScClazz scClazz)
    {
        List<ScClazz> list = scClazzService.find(scClazz);
        return AjaxResult.success(list);
    }

    /**
     * 导出班级列表
     */
    @PreAuthorize("@ss.hasPermi('study:clazz:export')")
    @Log(title = "班级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScClazz scClazz)
    {
        List<ScClazz> list = scClazzService.selectScClazzList(scClazz);
        ExcelUtil<ScClazz> util = new ExcelUtil<>(ScClazz.class);
        util.exportExcel(response, list, "班级数据");
    }

    /**
     * 获取班级详细信息包含学员和班级科目
     */
    @PreAuthorize("@ss.hasPermi('study:clazz:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(scClazzService.selectScClazzById(id));

    }

    /**
     * 查询班级包含学员和历史学员
     *
     * @param clazzId 班级主键
     * @return 班级
     */
    @PreAuthorize("@ss.hasPermi('study:clazz:query')")
    @GetMapping(value = "/selectScClazzByIdIncludeScStudent/{id}")
    public AjaxResult selectScClazzByIdIncludeScStudent(@PathVariable("id") Long clazzId) {
        return AjaxResult.success(scClazzService.selectScClazzByIdIncludeScStudent(clazzId));
    }

    /**
     * 新增班级
     */
    @PreAuthorize("@ss.hasPermi('study:clazz:add')")
    @Log(title = "班级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScClazz scClazz)
    {
        scClazzService.insertScClazz(scClazz);
        return toAjax(scClazz.getId().intValue());
    }

    /**
     * 升学
     */
    @PreAuthorize("@ss.hasPermi('study:clazz:enterHigherSchool')")
    @Log(title = "班级", businessType = BusinessType.INSERT)
    @PostMapping("/enterHigherSchool")
    public AjaxResult enterHigherSchool(@RequestBody ScClazz scClazz)
    {
        return AjaxResult.success(scClazzService.enterHigherSchool(scClazz));
    }

    /**
     * 修改班级
     */
    @PreAuthorize("@ss.hasPermi('study:clazz:edit')")
    @Log(title = "班级", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult update(@RequestBody ScClazz scClazz)
    {
        return toAjax(scClazzService.updateScClazz(scClazz));
    }

    /**
     * 修改班级和班级包含的所有外表
     */
    @PreAuthorize("@ss.hasPermi('study:clazz:edit')")
    @Log(title = "班级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScClazz scClazz)
    {
        return toAjax(scClazzService.updateScClazzIncludeStudent(scClazz));
    }


    /**
     * 删除班级
     */
    @PreAuthorize("@ss.hasPermi('study:clazz:remove')")
    @Log(title = "班级", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scClazzService.deleteScClazzByIds(ids));
    }


}
